package com.ybkj.o2o.mc.controller.v4;

import com.ybkj.o2o.mc.common.PhoneNumberHelper;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.entity.PassengerGrade;
import com.ybkj.o2o.mc.manager.PassengerGradeManager;
import com.ybkj.o2o.mc.manager.PassengerManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("all")
@RestController
@RequestMapping(value = "api/passengers/v4")
public class PassengerV4Res {
  @Autowired
  private PassengerManager passengerManager;

  @Autowired
  private PassengerGradeManager passengerGradeManager;

  /**
   * 根据电话号码查询乘客信息.
   *
   * @param phone  电话号码
   * @param appKey 系统key
   */
  @RequestMapping(value = "findone", method = RequestMethod.GET)
  public ResultTransport getPassenger(
          @RequestParam("phone") String phone,
          @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey) || StringUtils.isBlank(phone)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    Passenger passenger = passengerManager.findByPhoneAndAppKey(phone, appKey);
    if (passenger == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PASSENGER_NOT_EXISTS);
    }

    Map<String, Object> map = new HashMap<>();
    map.put("passengerId", passenger.getId());
    map.put("passengerName", passenger.getName());
    map.put("passengerPhone", passenger.getPhone());

    ResultTransport resultTransport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
    resultTransport.setData(map);

    return resultTransport;
  }

  /**
   * 根据主键查询乘客信息.
   *
   * @param passengerId 乘客主键
   * @param appKey      系统key
   */
  @RequestMapping(value = "findById", method = RequestMethod.GET)
  public ResultTransport findById(
          @RequestParam("passengerId") Long passengerId,
          @RequestParam("appKey") String appKey) {
    if (StringUtils.isBlank(appKey) || passengerId == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    if (passenger == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PASSENGER_NOT_EXISTS);
    }

    ResultTransport resultTransport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
    resultTransport.setData(passenger);

    return resultTransport;
  }

  /**
   * 新建客户.
   */
  @RequestMapping(value = "create", method = RequestMethod.POST)
  public ResultTransport create(
          @RequestParam("phone") String phone,
          @RequestParam("companyId") Long companyId,
          @RequestParam("companyName") String companyName,
          @RequestParam("appKey") String appKey,
          @RequestParam(required = false, value = "name") String name,
          @RequestParam(required = false, value = "channel_name") String channelName) {
    phone = StringUtils.trimToEmpty(phone);
    companyName = StringUtils.trimToEmpty(companyName);
    appKey = StringUtils.trimToEmpty(appKey);
    if (StringUtils.isBlank(phone) || companyId == null || companyId == 0
            || StringUtils.isBlank(companyName) || StringUtils.isBlank(appKey)) {

      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    boolean number = PhoneNumberHelper.isPhoneNumber(phone);
    if (!number) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PHONE_FORMAT_ERROR);
    }

    boolean exists = passengerManager.existsByPhone(phone, appKey);
    if (exists) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PASSENGER_EXISTS);
    }

    try {
      Passenger passenger = new Passenger();
      if (StringUtils.isNotBlank(name)) {
        if (name.length() > 10) {
          return ResultTransport.getErrorBy(ErrorConstantV4.PASSENGER_NAME_TOOLONG);
        }
        passenger.setName(name);
      } else {
        passenger.setName("先生/女士");
      }
      passenger.setPhone(phone);
      passenger.setAllowOverdraw(false);
      passenger.setInBlackList(false);
      passenger.setType(0);
      passenger.setGender(0);
      if (StringUtils.isNotBlank(channelName)) {
        passenger.setChannelName(channelName);
      }
      passenger.setCompanyId(companyId);
      passenger.setCompanyName(companyName);
      passenger.setAppKey(appKey);
      passenger.setDrawMoney(0.0);
      passenger.setNodrawMoney(0.0);
      passenger.setFrozenMoney(0.0);
      passenger.setReceiptMoney(0.0);

      PassengerGrade defaultGrade = passengerGradeManager.findDefault(companyId,appKey);
      if (null != defaultGrade) {
        passenger.setGradeId(defaultGrade.getId());
        passenger.setGradeName(defaultGrade.getName());
      }

      passenger = passengerManager.insert(passenger);


      Map<String, Object> map = new HashMap<>();
      map.put("passengerId", passenger.getId());
      map.put("passengerName", passenger.getName());
      map.put("passengerPhone", passenger.getPhone());
      ResultTransport transport = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      transport.setData(map);
      return transport;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 批量添加客户（导入）.
   * @param passengers 客户
   * @param appKey appKey
   */
  @RequestMapping(value = "batchCreate", method = RequestMethod.POST)
  @ResponseBody
  public ResultTransport batchCreate(
          @RequestParam("passengers") String passengers,
          @RequestParam("appKey") String appKey) {

    try {
      Map<String,Object> map = passengerManager.batchCreate(passengers,appKey);
      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(map);
      return instance;
    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }
}
